#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    isConnect=false;
    startSend=false;
    mode=false;
    left_pressed=false;
    right_pressed=false;

    tcpServer = NULL;
    tcpSocket = NULL;
    ui->car->move(100,900);

    clickTimes=0;

    ui->left->setText(tr("LEFT"));
    ui->left->setVisible(false);

    ui->right->setText(tr("RIGHT"));
    ui->right->setVisible(false);

    rotationSensor = new QRotationSensor(this);
    rotationSensor->start();

    QList<QHostAddress> addr = QNetworkInterface::allAddresses();
    ui->info1->setText(addr.at(0).toString());
    ui->info2->setText(addr.at(1).toString());

    tcpServer = new QTcpServer(this);
    tcpServer->listen(QHostAddress::Any,8333);

    connect(tcpServer,&QTcpServer::newConnection,[=](){
        //取出建立好连接的的套接字
        tcpSocket = tcpServer->nextPendingConnection();

        //获取对方的IP和端口
        QString ip = tcpSocket->peerAddress().toString();
        qint16  port = tcpSocket->peerPort();
        QString temp = QString("Client: [%1:%2]").arg(ip).arg(port);
        ui->info3->setText(temp);
        isConnect=true;

        connect(tcpSocket, &QTcpSocket::readyRead,[=](){
             QByteArray buf = tcpSocket->readAll();
             cout <<buf.data()<<endl;
         });

    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *)
{

}

void MainWindow::on_pushButton_clicked()
{

}

void MainWindow::send_str(char sendBuf[])
{
    if(!isConnect)
        return ;
    int send_len = 0;
    int temp=0;
    while(temp++<10)
    {
        send_len = tcpSocket->write(sendBuf, 32);
        if (send_len < 0) {
            qDebug() <<"try times "<<temp<< ": 发送失败！" << endl;
        }
        else
            break;
    }
}

void MainWindow::update_text()
{
    rotationReading = rotationSensor->reading();
    double x=rotationReading->x();
    double y=rotationReading->y();

    int ry=0;

    if(x<-25)
        x=-25;
    else if(x>25)
        x=25;
    ry=x*(this->height()/100)+this->height()/2;

    int rx=0;
    if(y<=-70)
        y=-70;
    else if(y>-10)
        y=-10;
    rx=(70+y)*(this->width()-200)/60.0;

    x=x/25.0;
    y=(y+70)/60.0;


    char str[32];
    if(!mode)
    {
        sprintf(str,"%lf %lf",x,y);
        send_str(str);
        ui->car->move(100+rx,ry);
    }
    else
    {
        if(left_pressed)
            sprintf(str,"-1 %lf",y);
        else if(right_pressed)
            sprintf(str,"1 %lf",y);
        else
            sprintf(str,"0 %lf",y);
        send_str(str);
        ui->car->move(100+rx,this->height()/2-ui->car->height()/2);
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    if(!startSend)
    {
        startSend=true;
        ui->pushButton_2->setText(tr("stop"));
        m_timer = new QTimer(this);
        //设置定时器是否为单次触发。默认为 false 多次触发
        m_timer->setSingleShot(false);
        //启动或重启定时器, 并设置定时器时间：毫秒
        m_timer->start(50);
        //定时器触发信号槽
        connect(m_timer, SIGNAL(timeout()), this, SLOT(update_text()));
    }
    else
    {
        ui->pushButton_2->setText(tr("start"));
        m_timer->stop();
        startSend=false;
    }
}

void MainWindow::on_car_clicked()
{
    clickTimes++;
    if(clickTimes>2)
        clickTimes=0;

    if(clickTimes==1)
    {
        mode=true;
        ui->car->setText(tr("M1"));
        ui->left->setVisible(true);
        ui->right->setVisible(true);
        ui->left->setGeometry(this->width()*0.25,this->height()*0.125,this->width()*0.5,this->height()*0.25);
        ui->line->setGeometry(0,this->height()/2,this->width(),2);
        ui->right->setGeometry(this->width()*0.25,this->height()*0.625,this->width()*0.5,this->height()*0.25);
    }
    else if(clickTimes==2)
    {
        mode=true;
        ui->car->setText(tr("M2"));
        ui->left->setVisible(true);
        ui->right->setVisible(false);
        ui->left->setGeometry(this->width()*0.25,this->height()*0.125,this->width()*0.5,this->height()*0.75);
        ui->line->setGeometry(0,this->height()/2,this->width(),2);
    }
    else
    {
        mode=false;
        ui->car->setText(tr("M0"));
        ui->left->setVisible(false);
        ui->right->setVisible(false);
    }
}


void MainWindow::on_left_pressed()
{
    if(clickTimes==1)
    {
        char str[32];
        sprintf(str,"-1 -2");
        send_str(str);
        left_pressed=true;
        ui->left->setText(tr("press"));
    }
    else if(clickTimes==2)
    {
        QCursor cursor;
        cursor=this->cursor();
        int h=cursor.pos().y();
        if(h>this->height()/2)
        {
            left_pressed=false;
            right_pressed=true;
            ui->left->setText(tr("Right Press"));
//            cout <<h<<" * "<<this->height()/2<<endl;
        }
        else
        {
            right_pressed=false;
            left_pressed=true;
            ui->left->setText(tr("Left Press"));
//            cout <<h<<" - "<<this->height()/2<<endl;
        }

    }
}

void MainWindow::on_left_released()
{
    char str[32];
    sprintf(str,"0 -2");
    send_str(str);
    left_pressed=false;
    if(clickTimes==2)
        right_pressed=false;
}

void MainWindow::on_right_pressed()
{
    char str[32];
    sprintf(str,"1 -2");
    send_str(str);
    ui->right->setText(tr("press"));
    right_pressed=true;
}

void MainWindow::on_right_released()
{
    char str[32];
    sprintf(str,"0 -2");
    send_str(str);
    ui->right->setText(tr("release"));
    right_pressed=false;
}
